package org.ourgrid.common.statistics.control;

import java.io.IOException;
import java.util.Map;

import org.ourgrid.common.statistics.beans.peer.Peer;
import org.ourgrid.common.statistics.beans.peer.User;
import org.ourgrid.common.statistics.util.hibernate.HibernateUtil;
import org.ourgrid.peer.dao.UsersDAO;
import org.ourgrid.peer.to.PeerUser;

import br.edu.ufcg.lsd.commune.container.servicemanager.ServiceManager;

public class UserControl extends EntityControl {
	
	private static UserControl instance = null;
	
	public static UserControl getInstance() {
		if (instance == null) {
			instance = new UserControl();
		}
		return instance;
	}
	
	protected UserControl() {}
	
	public User addUser(String login, String password, ServiceManager serviceManager, UsersDAO userDAO) {
		logger.enter();
		
		HibernateUtil.beginTransaction();
		
		User user = null;
		try {
			user = userDAO.addUser(login.toLowerCase(), password, serviceManager);
			HibernateUtil.commitTransaction();
		} catch (Exception e) {
			HibernateUtil.rollbackAndCloseSession();
		}
		
		logger.leave();
		return user;
	}
	
	public User insertUser(String login, String password, UsersDAO userDAO, Peer peer) {
		logger.enter();
		
		HibernateUtil.beginTransaction();
		
		User user = null;
		try {
			user = userDAO.insertUser(login, password, peer);
			HibernateUtil.commitTransaction();
		} catch (Exception e) {
			HibernateUtil.rollbackAndCloseSession();
		}
		
		logger.leave();
		return user;
	}

	public void removeUser(String login, UsersDAO userDAO) {
		logger.enter();
		
		HibernateUtil.beginTransaction();
		
		try {
			userDAO.removeUser(login);
			HibernateUtil.commitTransaction();
		} catch (Exception e) {
			HibernateUtil.rollbackAndCloseSession();
		}
		
		logger.leave();
	}
	
	public void registerPublicKey(PeerUser user, String publicKey, UsersDAO userDAO) throws IOException {
		HibernateUtil.beginTransaction();
		
		try {
			userDAO.registerPublicKey(user, publicKey);
			HibernateUtil.commitTransaction();
		} catch (Exception e) {
			HibernateUtil.rollbackAndCloseSession();
		}
	}
	
	public boolean userExists(String userPublicKey, UsersDAO userDAO){
		HibernateUtil.getSession();
		HibernateUtil.beginTransaction();
		
		boolean exists = userDAO.userExists(userPublicKey);
		HibernateUtil.closeSession();
		return exists;
	}
	
	public PeerUser getUser(String login, UsersDAO userDAO) {
		HibernateUtil.getSession();
		HibernateUtil.beginTransaction();
		
		PeerUser user = userDAO.getUser(login);
		HibernateUtil.closeSession();
		return user;
	}
	
	public PeerUser getUserByPublicKey(String userPublicKey, UsersDAO userDAO) {
		HibernateUtil.getSession();
		HibernateUtil.beginTransaction();
		
		PeerUser user = userDAO.getUserByPublicKey(userPublicKey);
		HibernateUtil.closeSession();
		return user;
	}
	
	public Map<String,PeerUser> getUsers(UsersDAO userDAO){
		HibernateUtil.getSession();
		HibernateUtil.beginTransaction();
		
		Map<String,PeerUser> users = userDAO.getUsers();
		HibernateUtil.closeSession();
		return users;
	}

}
